//
//  ClassPhotoViewController.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/11/28.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "ClassPhotoViewController.h"

#import "PhotoDetaiModel.h"

#import "ClassPhotoCell.h"
@interface ClassPhotoViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    int pageNum;
    
    BOOL isRefresh;
    
    EmptyStateView *emptyView;//空状态
}

@property (nonatomic,strong) UITableView *table;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *imagesArray;

@end

@implementation ClassPhotoViewController

- (UITableView *)table
{
    if (_table == nil) {
        
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    
    return _table;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBar.nav_titleLabel.text = _className;
    
    [self initArray];
    
    [self requestPhotosData];
    
    [self loadTableView];
    // Do any additional setup after loading the view.
}
- (void)initArray
{
    _dataArray = [NSMutableArray array];
    _imagesArray = [NSMutableArray array];
}
- (void)loadTableView
{

    [self.view addSubview:self.table];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"6" andBottomStr:@"相册暂无图片"];
    [_table addSubview:emptyView];
    
    _table.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        
        [self requestPhotosData];
    }];
    
    _table.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        
        [self requestPhotosMoreData];
    }];
}

- (void)requestPhotosData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:@"api_dynamicphoto_listbyclass" forKey:@"method"];
    [param setObject:_classid forKey:@"classid"];
    [param setObject:@"1" forKey:@"pageno"];
    [param setObject:@10 forKey:@"pagesize"];
    
    [Utils get:nil parameters:param success:^(NSInteger recode, id jsonObject) {

        [self->_dataArray removeAllObjects];
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        if (array.count==0) {
            
            [self->emptyView showEmptyImage];
            [self->_table.mj_header endRefreshing];
            self->_table.tableFooterView = [[UIView alloc]init];
            [self->_table reloadData];
            
            return ;
        }else if(array.count ==10)
        {
            
            self->isRefresh = YES;
            
        }else
        {
            self->isRefresh = NO;
            
        }
        
        [self->emptyView hideEmptyImage];
        
        self->pageNum = 2;
        
        for (NSDictionary *dic in array) {
            
            PhotoDetaiModel *model = [PhotoDetaiModel mj_objectWithKeyValues:dic];
            [self->_dataArray addObject:model];
        }
        
        [self->_table reloadData];
        [self->_table.mj_header endRefreshing];
    } failure:^(NSInteger recode, NSError *error) {
        
    }];

}

- (void)requestPhotosMoreData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:@"api_dynamicphoto_listbyclass" forKey:@"method"];
    [param setObject:_classid forKey:@"classid"];
    [param setObject:@(pageNum) forKey:@"pageno"];
    [param setObject:@10 forKey:@"pagesize"];
    
    [Utils get:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        if(array.count == 10)
        {
            self->pageNum ++;
            self->isRefresh = YES;
        }else
        {
            self->isRefresh = NO;
        }
        
        for (NSDictionary *dic in array) {
            
            PhotoDetaiModel *model = [PhotoDetaiModel mj_objectWithKeyValues:dic];
            [self->_dataArray addObject:model];
        }
        
        [self->_table reloadData];
        [self->_table.mj_footer endRefreshing];
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
#pragma mark - UITableviewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoDetaiModel *modle = [_dataArray objectAtIndex:indexPath.row];
    
    NSArray * infoFileList = modle.infofilelist;
    
    NSMutableArray *fileids = [NSMutableArray array];
    
    for (NSDictionary * dic in infoFileList) {
        
        NSString * fileidsStr = [[dic objectForKey:@"fileid"] stringValue];
        
        if (fileidsStr.length != 0) {
            
            [fileids addObject:fileidsStr];
            
        }
    
    }
    
    NSInteger line = 0;
    if ([fileids count]%3 == 0) {
        line = [fileids count]/3;
    }else
    {
        line = [fileids count]/3 + 1;
    }
    
    float w = (SCREENWIDTH-30-18)/3;
    
    return ((9 + w) * line - 9)+71+0.5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PhotoDetaiModel *model = [_dataArray objectAtIndex:indexPath.row];
    
    ClassPhotoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
    if (cell == nil) {
        
        cell = [[ClassPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
    }
    
    
    [cell setDataForCellWith: model classNameWith:_className withType:@"photo"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (isRefresh == NO) {
        
        UIView *view =[[ UIView alloc] initWithFrame:CGRectMake(0, _table.contentSize.height, VIEW_WIDTH, 60)];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 0.4)];
        lineView.backgroundColor = RGBACOLOR(222, 223, 229, 1);
        
        UILabel *label=[[UILabel alloc]init];
        label.frame= CGRectMake(0, 0.4, VIEW_WIDTH, 59.6);
        
        label.text = @"没有更多内容了";
        label.font = [UIFont boldSystemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        
//        [label addSubview:lineView];
        
        [view addSubview:label];
        label.textColor = [UIColor colorWithHexString:@"979797"];
        _table.tableFooterView = view;
        _table.mj_footer = nil;
        
    }else if (isRefresh == YES){
        
        _table.tableFooterView = nil;
        
        _table.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self requestPhotosMoreData];
            
        }];
    }
    //end of loading //for example [activityIndicator stopAnimating]; }}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
